<template>
	<om-formsub-outer :index="index" :customDelFunc="true" @save="save" @delete="del" confirmBtnText="确认" deleteBtnText="放弃">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		errorModal
	} from '@/static/js/common.js';
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	import moment from'@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
					tkeyWorkListId:'',
					yearOrderNum3:"",
					bearUser3Id:'',
					bearUser3Name:'',
					id:""
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				workName3Data:[],
				orderNumCacheKey:'pm-work-report-ordernum-cache'
			}
		},
		methods: {
			onSelectDelayEnd(date){
				let _that = this
				let now = moment().format('YYYY-MM-DD')
				if(moment(date).isSameOrBefore(now)){
					errorModal(_that,'提示信息','请选择'+moment().format('YYYY-MM-DD')+'之后的时间',()=>{
						_that.$refs.autoform.clear("itemInfo","delayEndDate")
					})
				}
			},
			onSelectEvaluateResult(item){
				let _that = this
				_that.$refs.autoform.formdata.evaluateResultCode = item.code
				if(item.code == '39374'){
					_that.$refs.autoform.changeEditor("itemInfo","delayEndDate","type","newdatebox")
				}else{
					_that.$refs.autoform.changeEditor("itemInfo","delayEndDate","type","labelbox")
					_that.$refs.autoform.clear("itemInfo","delayEndDate")
				}
			},
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
						key: this.cachename,
						data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				uni.navigateBack()
			},
			clearOrder(){
				var formdata  = this.$refs.autoform.formdata
				formdata.id = ""
				formdata.orderNum = ""
				formdata.itemName = ""
				formdata.workStandard = ""
				formdata.deliveryName = ""
				formdata.keyItem = ""
				formdata.planEndDate = ""
				formdata.evaluateUserName = ""
			},
			initConfig(e){
				let _that = this
				changeEditor(_that.autoformdata[0].editors,"score","min",0)
				changeEditor(_that.autoformdata[0].editors,"score","max",_that.formdata.maxScore)
				var slist = _that.datas.filter((e,i) => {
							return (i+1) != _that.order
						}).map(e=>e.orderNum + e.itemName+ e.posId + e.planEndDate);
				changeEditor(_that.autoformdata[0].editors,"orderNum","link","../../public/search/search")
				changeEditor(_that.autoformdata[0].editors, "orderNum", "linkParam", {
					other:JSON.stringify({
						editId:e.editId,
						zposId: e.zposId,
						instId: e.instId,
						userId: e.userId,
						sList: slist
					}),
					namefield:"itemName",
					notefield:"workStandard",
					extrafield:"planEndDate",
					searchfield:"q",
					initload:true,
					url:"tkeyWorkEvaluate/orderNumList2",
					cachename:_that.orderNumCacheKey
				})
				//附件
				changeEditor(_that.autoformdata[0].editors, "fileList", "busCode", "OBJ0211")
				changeEditor(_that.autoformdata[0].editors, "fileList", "fileUplaodKey",'94')
				changeEditor(_that.autoformdata[0].editors, "fileList", "formId", _that.formdata.tkeyWorkListId)
				
				let arr = [
					_that.om.request(
						'tsysdict/getCombobox',
						{
							code:'39371'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("itemInfo","evaluateResultId","data",configSelectData(res[0],'id','dicname'))
					_that.$refs.autoform.initSelectIndex("itemInfo","evaluateResultId")
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig(e)
								if(!_that.formdata.evaluateResultId || _that.formdata.evaluateResultCode != '39374'){
									changeEditor("itemInfo","delayEndDate","type","labelbox",_that.autoformdata)
								}else{
									changeEditor("itemInfo","delayEndDate","type","newdatebox",_that.autoformdata)
								}
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.orderNumCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.id = d.id
							formdata.posId = d.posId
							formdata.orderNum = d.orderNum
							formdata.itemName = d.itemName
							formdata.workStandard = d.workStandard
							formdata.deliveryName = d.deliveryName
							formdata.keyItem = d.keyItem
							formdata.planEndDate = d.planEndDate
							formdata.evaluateUserName = d.evaluateUserName
							formdata.maxScore = d.maxScore
							formdata.actualEndDate = d.actualEndDate
							formdata.remark = d.remark
							formdata.tkeyWorkListId = d.tkeyWorkListId
							//附件
							formdata.fileList = d.fileList
							_that.$refs.autoform.changeEditor("itemInfo", "fileList", "subId", d.tkeyWorkListId)
							uni.removeStorage({
								key: _that.orderNumCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
